home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.centrum.is!news
- From: bjarnir@centrum.is (Bjarni Ragnarsson)
- Newsgroups: comp.lang.c++
- Subject: Re: HELP with a simple C Structure
- Date: 3 Apr 1996 15:37:50 GMT
- Organization: BR Software
- Message-ID: <4ju60e$lsj@newshost.centrum.is>
- References: <4jm38u$j1a@news.bellglobal.com>
- NNTP-Posting-Host: tungl-100.centrum.is
- X-Newsreader: WinVN version 0.82
-
- In article <4jm38u$j1a@news.bellglobal.com>, bogdanfl@aei.ca (Bogdan Florescu) says:
- >
- >I need to do this, and I don't khow how.
- >
- >struct Employee
- > {
- > float salary;
- > char department;
- > }
- >
- >Employee Jim, Paul, Pat, Scott;
- >
- >void main(void)
- > {
- > Jim.salary=15;
- > Paul.salary=14;
- > Pat.salary=13;
- > Scott.salary=12;
- >
- > Jim.department='a';
- > Paul.department='b';
- > Pat.department='c';
- > Scott.department='b';
- > ................................
- > }
- >
- >I need to replace the dots with a program that tells me who else works
- >in Paul's department and how much money he makes.
- >
- >I would like to have the structure variables named with the name of
- >the employee, (instead of an array with the name of the employee as a
- >member) so I can easily access their members. The structure can be
- >modified, but I really need to be able to have expressions such as
- >Jim.salary.
- >
- >Thank you,
- >Bogdan Florescu
-
- Can't see how you could do that unless you wan't to check each struct-variable
- with an "if" statement for instance.
- Example:
- if (Jim.department == Paul.department)
- printf("Jim\n"); //or whatever you want to do with people
- //working in same department.
- if (Pat.department==Paul.department)
- printf("Pat\n");
-
- ...etc.
-
- I think you have to define the variables as an array if you wan't to do this in a more
- general way. Then you would include a name field in the struct.
- // d is Pauls department in this example.
- for (i=0;i<max;i++)
- if (EmployeArray[i].department == d )
- printf("%s\n",EmployeArray[i].name);
-
- or something like that.
-
- If you wish to refer to specific employes by name (in your code), you could always
- define a number for each employe in the array.
-
- enum EmployeNumber {Jim=0,Paul,Pat,Scott};
-
- and refer to an employe as
- EmployeArray[Paul].department
- EmployeArray[Paul].salary
-
- etc.
-
- A bit messy but ......
-
- Maybe I misunderstood your question in the first place ?
-
- Bjarni Ragnarsson
-